<?php

namespace tools;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use think\facade\Env;

class Email
{
    protected $Host;
    protected $SMTPAuth;
    protected $Username;
    protected $Password;
    protected $Port;
    protected $mail;

    public function __construct()
    {
        $this->Host = Env::get('SMTP_HOST');
        $this->SMTPAuth = Env::get('SMTP_AUTH');
        $this->Username = Env::get('SMTP_USERNAME');
        $this->Password = Env::get('SMTP_PASSWORD');
        $this->SMTPSecure = Env::get('SMTP_SECURE');
        $this->Port = Env::get('SMTP_PORT');

        $this->mail = new PHPMailer;
        $this->mail->isSMTP();
        $this->mail->Host = $this->Host;
        $this->mail->SMTPAuth = true;
        $this->mail->Username   = $this->Username;
        $this->mail->Password   = $this->Password;
        $this->mail->SMTPSecure = $this->SMTPSecure;
        $this->mail->Port       = 465;
    }

    public function SendMail($to, $subject, $content, $isHTML = true)
    {
        $this->mail->isHTML($isHTML);
        $this->mail->Subject = $subject;
        $this->mail->Body = $content;

        $this->mail->setFrom($this->Username, 'EvoForum');
        $this->mail->addAddress($to);

        try {
            $this->mail->send();
            return true;
        } catch (Exception $e) {
            trace("Message could not be sent. Mailer Error: {$this->mail->ErrorInfo}", 'error');
            return false;
        }
    }
}
